package com.loong.seller.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.common.rabbitmq.dto.base.RepositoryEventDto;
import com.loong.common.utils.TransformUtils;
import com.loong.seller.common.backend.param.ShopParam;
import com.loong.seller.common.backend.param.ShopQueryParam;
import com.loong.seller.common.backend.param.ShopReportParam;
import com.loong.seller.mapper.ShopMapper;
import com.loong.seller.model.Shop;
import com.loong.seller.mq.ShopReport;
import com.loong.seller.result.ShopDetail;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: hzp
 * @Date: 2020-10-26 11:43
 */
@Repository
public class ShopBusiness extends AbstractBusiness<Shop> {
    @Resource
    ShopMapper shopMapper;

    @Resource
    ShopReport shopReport;

    public ShopDetail findShopDetailByShopId(Long shopId){
        return shopMapper.findShopDetailByShopId(shopId);
    }

    /**
     * 分页查询
     * @param param 查询条件
     * @return 返回店铺
     */
    public List<Shop> query(ShopQueryParam param){
        Condition condition = new Condition(Shop.class);
        Example.Criteria criteria = condition.createCriteria();
        buildQueryCriteria(param,criteria);
        return findByCondition(condition);
    }
    //这里根据业务需求构造条件
    private void buildQueryCriteria(ShopQueryParam param,Example.Criteria criteria){
        if(ObjectUtils.isNotEmpty( param.getName())){
            criteria.andEqualTo("name", param.getName());
            // criteria.andLike("name",param.getName());
        }
        if(ObjectUtils.isNotEmpty(param.getAddress())){
            criteria.andLike("address",param.getName());
        }
        if(ObjectUtils.isNotEmpty(param.getType())){
            criteria.andEqualTo("type",param.getType());
        }
        if(ObjectUtils.isNotEmpty(param.getAdmin())){
            criteria.andEqualTo("admin",param.getAdmin());
        }
    }


    public void updateByPrimaryKeySelective(ShopParam param ) {
        shopMapper.updateByPrimaryKeySelective(TransformUtils.transform(param,Shop.class));
        ShopReportParam shopParam = TransformUtils.transform(param, ShopReportParam.class);
        //todo 店铺信息完善.... 评分 月售等
        //上报mq
        RepositoryEventDto<ShopReportParam> shopRepositoryEventDto = RepositoryEventDto.buildWithUpdate(shopParam);
        shopReport.report(shopRepositoryEventDto);
    }


    public void save(Shop model) {
        shopMapper.insertSelective(model);
        ShopReportParam shopParam = TransformUtils.transform(model, ShopReportParam.class);
        //todo 店铺信息完善.... 评分 月售等
        //上报mq
        RepositoryEventDto<ShopReportParam> shopRepositoryEventDto = RepositoryEventDto.buildWithCreate(shopParam);
        shopReport.report(shopRepositoryEventDto);
    }


    public void deleteById(Long id) {
        shopMapper.deleteByPrimaryKey(id);
        Shop shop = new Shop();
        shop.setId(id);
        //上报mq
        RepositoryEventDto<Shop> longRepositoryEventDto = RepositoryEventDto.buildWithDelete(shop);
        shopReport.report(longRepositoryEventDto);
    }

    public void saveShop(ShopParam param) {
        Shop shop = TransformUtils.transform(param, Shop.class);
        shopMapper.insertSelective(shop);
        param.setId(shop.getId());
        ShopReportParam shopReportParam=TransformUtils.transform(param, ShopReportParam.class);
        //todo 店铺信息完善.... 评分 月售等
        //上报店铺里面包括配送方式
        RepositoryEventDto<ShopReportParam> longRepositoryEventDto = RepositoryEventDto.buildWithCreate(shopReportParam);
        shopReport.report(longRepositoryEventDto);

    }

    public List<Shop> findUserShopCollectionByUserId(Long userId) {
        return shopMapper.findUserShopCollectionByUserId(userId);
    }
}
